﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;

namespace SearchAdminToolkit.UI.Controls
{
    /// <summary>
    /// Defines a base class for all toolkit controls.
    /// </summary>
    public abstract class BaseToolkitControl : WebControl
    {

        #region Constants

        private const string ADMIN_ICON_URL = "/_layouts/images/manageuser.gif";

        #endregion

        #region Fields

        private LiteralControl _errorLabel;

        #endregion

        #region Properties

        /// <summary>
        /// Gets a label for displaying error information.
        /// </summary>
        protected LiteralControl ErrorLabel
        {
            get
            {
                if (_errorLabel == null)
                {
                    _errorLabel = new LiteralControl();
                    _errorLabel.EnableViewState = false;
                }
                return _errorLabel;
            }
        }
        #endregion

        #region Methods

        /// <summary>
        /// Allows derived controls to define the section title.
        /// </summary>
        /// <returns></returns>
        protected abstract string GetSectionTitle();

        /// <summary>
        /// Allows derived controls to provide the section description.
        /// </summary>
        /// <returns></returns>
        protected abstract string GetSectionDescription();

        /// <summary>
        /// Allows derived controls to render the core toolkit.
        /// </summary>
        /// <param name="writer"></param>
        protected abstract void RenderToolkitControl(HtmlTextWriter writer);

        /// <summary>
        /// Sets the current error message.
        /// </summary>
        /// <param name="message"></param>
        protected void SetErrorMessage(string message)
        {
            this.ErrorLabel.Text = string.Format("<div style=\"color:red;\">{0}</div>", message);
            this.ErrorLabel.Visible = true;
        }

        /// <summary>
        /// Overridden to create the composite toolkit control.
        /// </summary>
        protected override void CreateChildControls()
        {
            this.ErrorLabel.Visible = false;
            this.Controls.Add(this.ErrorLabel);
        }

        /// <summary>
        /// Overridden to render the composite control.
        /// </summary>
        /// <param name="writer"></param>
        protected override void RenderContents(System.Web.UI.HtmlTextWriter writer)
        {
            writer.Write(this.GenerateStyles());

            writer.Write(this.CreateSectionHeader(this.GetSectionTitle()));

            // <table>
            writer.AddAttribute(HtmlTextWriterAttribute.Width, "100%");
            writer.RenderBeginTag(HtmlTextWriterTag.Table);
            // <tr>
            writer.RenderBeginTag(HtmlTextWriterTag.Tr);

            // <td>
            writer.AddAttribute(HtmlTextWriterAttribute.Valign, "top");
            writer.AddAttribute(HtmlTextWriterAttribute.Width, "1%");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);

            // <img />
            writer.AddAttribute("hspace", "8");
            writer.AddAttribute("vspace", "5");
            writer.AddAttribute(HtmlTextWriterAttribute.Src, ADMIN_ICON_URL);
            writer.RenderBeginTag(HtmlTextWriterTag.Img);
            writer.RenderEndTag();

            // </td>
            writer.RenderEndTag();

            // <td>
            writer.AddAttribute(HtmlTextWriterAttribute.Valign, "top");
            writer.AddAttribute(HtmlTextWriterAttribute.Class, "ms-descriptiontext");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);

            // error info
            this.ErrorLabel.RenderControl(writer);

            writer.Write(this.GetSectionDescription());

            this.RenderToolkitControl(writer);

            // </td>
            writer.RenderEndTag();
            // </tr>
            writer.RenderEndTag();
            // </table>
            writer.RenderEndTag();

        }

        /// <summary>
        /// Generates a properly-formatted section header.
        /// </summary>
        /// <param name="title"></param>
        /// <returns></returns>
        protected string CreateSectionHeader(string title)
        {
            return string.Format("<table width=\"100%\"><tr><td class=\"ms-sectionheader\">{0}</td></tr><tr><td class=\"ms-sectionline\"></td></tr></table>", title);
        }


        /// <summary>
        /// Generates a style to control the pager.
        /// </summary>
        /// <returns></returns>
        private string GenerateStyles()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<style>");
            sb.Append("table.sat-grid td");
            sb.Append("{");
            sb.Append(" border-bottom:1px solid #d9d9d9;");
            sb.Append("}");
            sb.Append("tr.sat-pager td");
            sb.Append("{");
            sb.Append(" font-family:arial;");
            sb.Append(" font-size:8pt;");
            sb.Append(" border-bottom:0px solid white;");
            sb.Append("}");
            sb.Append("</style>");

            return sb.ToString();
        }

        #endregion

    }
}
